%E[spend|purchase,x,treatment] - predicted amount by person x, assuming
%that conversion = 1, and given a specific treatment
% b: coefficients from the regression
% y: the amount spent in each example (last column)
% x: all the parameters from each example (every column except the last 2,
%    and the 3rd)
% k: the regularization term

M = dlmread('ParsedData.csv');

m0c = 1;
m1c = 1;
m2c = 1;
M0 = zeros(21306,29);
M1 = zeros(21307,29);
M2 = zeros(21387,29);
for i=1:64000
    if(M(i,9)==0)
        M0(m0c,:) = getcustomer(M,i); % no email
        M0(m0c,end) = M(i,end);
        m0c = m0c + 1;
    end
    if(M(i,9)==1)
        M1(m1c,:) = getcustomer(M,i); % no email
        M1(m1c,end) = M(i,end);
        m1c = m1c + 1;
    end
    if(M(i,9)==2)
        M2(m2c,:) = getcustomer(M,i); % no email
        M2(m2c,end) = M(i,end);
        m2c = m2c + 1;
    end
    if(mod(i,4000) ==0)
        i
    end
end

step = .01;
mu = .001;

% M0 = M(M(:,end-5) == 1,:); % no email
% M1 = M(M(:,end-4) == 1,:); % mens email
% M2 = M(M(:,end-3) == 1,:); % womens email
% 
% M0 = M0(M0(:,end-1)==1,:);
% M1 = M1(M1(:,end-1)==1,:);
% M2 = M2(M2(:,end-1)==1,:);

M0 = M0(M0(:,end)>0,:);
M1 = M1(M1(:,end)>0,:);
M2 = M2(M2(:,end)>0,:);
for k=0.1:0.1:0.1
b0 = ridge(M0(:,end),[M0(:,1:end-1) ones(size(M0,1),1)],k);
b1 = ridge(M1(:,end),[M1(:,1:end-1) ones(size(M1,1),1)],k);
b2 = ridge(M2(:,end),[M2(:,1:end-1) ones(size(M2,1),1)],k);

M0est = zeros(size(M0,1),1);
for i=1:size(M0,1)
    M0vect = [M0(i,1:end-1) 1]';
    M0est(i,1) = sum(M0vect .* b0);
end
diff0 = M0(:,end) - M0est(:);

M1est = zeros(size(M1,1),1);
for i=1:size(M1,1)
    M1vect = [M1(i,1:end-1) 1]';
    M1est(i,1) = sum(M1vect .* b1);
end
diff1 = M1(:,end) - M1est(:);

M2est = zeros(size(M2,1),1);
for i=1:size(M2,1)
    M2vect = [M2(i,1:end-1) 1]';
    M2est(i,1) = sum(M2vect .* b2);
end
diff2 = M2(:,end) - M2est(:);

 [mean(M0est) mean(M1est) mean(M2est)]
 [mean(M0(:,end)) mean(M1(:,end)) mean(M2(:,end))]
end
M_ = M;
M0_ = M0;
M1_ = M1;
M2_ = M2;

% figure;plot(diff0);
% figure;plot(diff1);
% figure;plot(diff2);

% p(visit|x,treatment)
M = dlmread('ParsedData.csv');
M0 = M(M(:,9) == 0,:); % no email
M1 = M(M(:,9) == 1,:); % mens email
M2 = M(M(:,9) == 2,:); % womens email

% visitbeta0 = zeros(1,7);
% visitbeta1 = zeros(1,7);
% visitbeta2 = zeros(1,7);
% customer = zeros(1,7);
visitbeta0 = zeros(1,29);
visitbeta1 = zeros(1,29);
visitbeta2 = zeros(1,29);
betahist = zeros(10*size(M0,1),29);
counter = 1;
vlcl = zeros(20,3);
for j=1:20
    for i=1:size(M0,1)
        customer = getcustomer(M0,i);
        p = sigmoid(visitbeta0.*customer);
        for k=1:size(visitbeta0,2)
            visitbeta0(k) = visitbeta0(k) + step*((M0(i,10) - p)*customer(k) - mu*visitbeta0(k));
        end
        betahist(counter,:) = visitbeta0;
        if(M0(i,10) == 1)
            vlcl(j,1) = vlcl(j,1) + log(p);
        elseif(M0(i,10) == 0)
            vlcl(j,1) = vlcl(j,1) + log(1-p);
        end
    end
    
    for i=1:size(M1,1)
        customer = getcustomer(M1,i);
        
        p = sigmoid(visitbeta1.*customer);
        for k=1:size(visitbeta1,2)
            visitbeta1(k) = visitbeta1(k) + step*((M1(i,10) - p)*customer(k) - mu*visitbeta1(k));
        end
        if(M1(i,10) == 1)
            vlcl(j,2) = vlcl(j,2) + log(p);
        elseif(M1(i,10) == 0)
            vlcl(j,2) = vlcl(j,2) + log(1-p);
        end
    end
    
    for i=1:size(M2,1)
        customer = getcustomer(M2,i);
        
        p = sigmoid(visitbeta2.*customer);
        for k=1:size(visitbeta2,2)
            visitbeta2(k) = visitbeta2(k) + step*((M2(i,10) - p)*customer(k) - mu*visitbeta2(k));
        end
        if(M2(i,10) == 1)
            vlcl(j,3) = vlcl(j,3) + log(p);
        elseif(M2(i,10) == 0)
            vlcl(j,3) = vlcl(j,3) + log(1-p);
        end
    end    
    j
end

figure;
plot(vlcl(:,1));
xlabel('Epoch');
ylabel('Log Likelihood');
title('P(visit = 1 | x, No Email)');
figure;
plot(vlcl(:,2));
xlabel('Epoch');
ylabel('Log Likelihood');
title('P(visit = 1 | x, Mens Email)');
figure;
plot(vlcl(:,3));
xlabel('Epoch');
ylabel('Log Likelihood');
title('P(visit = 1 | x, Womens Email)')

;
% Test calculated P(visit|x, treatment) values
visitProbs0 = zeros(size(M0(M0(:,10) == 1),1),1);
otherProbs0 = zeros(size(M0(M0(:,10) == 0),1),1);
visitProbs1 = zeros(size(M1(M1(:,10) == 1),1),1);
otherProbs1 = zeros(size(M1(M1(:,10) == 0),1),1);
visitProbs2 = zeros(size(M2(M2(:,10) == 1),1),1);
otherProbs2 = zeros(size(M2(M2(:,10) == 0),1),1);
visitc0 = 1;
otherc0 = 1;
visitc1 = 1;
otherc1 = 1;
visitc2 = 1;
otherc2 = 1;

for n=1:size(M,1)
    customer = getcustomer(M,n);
    visit = M(n,10);
    segment = M(n,9);
    
    if(segment == 0)
        if(visit == 1)
            visitProbs0(visitc0) = sigmoid(visitbeta0.*customer);
            visitc0 = visitc0 + 1;
        else
            otherProbs0(otherc0) = sigmoid(visitbeta0.*customer);
            otherc0 = otherc0 + 1;
        end
    end
    if(segment == 1)
        if(visit == 1)
            visitProbs1(visitc1) = sigmoid(visitbeta1.*customer);
            visitc1 = visitc1 + 1;
        else
            otherProbs1(otherc1) = sigmoid(visitbeta1.*customer);
            otherc1 = otherc1 + 1;
        end
    end
    if(segment == 2)
        if(visit == 1)
            visitProbs2(visitc2) = sigmoid(visitbeta2.*customer);
            visitc2 = visitc2 + 1;
        else
            otherProbs2(otherc2) = sigmoid(visitbeta2.*customer);
            otherc2 = otherc2 + 1;
        end
    end
end

visitProbs = [mean(otherProbs0), mean(visitProbs0); mean(otherProbs1), mean(visitProbs1); mean(otherProbs2), mean(visitProbs2)]

% p(purchase|visit,x,treatment)
M0 = M(M(:,9) == 0,:); % no email
M1 = M(M(:,9) == 1,:); % mens email
M2 = M(M(:,9) == 2,:); % womens email

M0 = M0(M0(:,end-2)==1,:);
M1 = M1(M1(:,end-2)==1,:);
M2 = M2(M2(:,end-2)==1,:);

purchasebeta0 = zeros(1,29);
purchasebeta1 = zeros(1,29);
purchasebeta2 = zeros(1,29);
lcl = zeros(20,3);
counter = 1;
step = 0.05
for j=1:20
    for i=1:size(M0,1)
        customer = getcustomer(M0,i);
        
        p = sigmoid(purchasebeta0.*customer);
        for k=1:size(purchasebeta0,2)
            purchasebeta0(k) = purchasebeta0(k) + step*((M0(i,end-1) - sigmoid(purchasebeta0.*customer))*customer(k) - mu*purchasebeta0(k));
        end
        if(M0(i,end-1) == 1)
            lcl(j,1) = lcl(j,1) + log(p);
        elseif(M0(i,end-1) == 0)
            lcl(j,1) = lcl(j,1) + log(1-p);
        end
    end


    for i=1:size(M1,1)
        customer = getcustomer(M1,i);

        p = sigmoid(purchasebeta1.*customer);
        for k=1:size(purchasebeta1,2)
            purchasebeta1(k) = purchasebeta1(k) + step*((M1(i,end-1) - sigmoid(purchasebeta1.*customer))*customer(k) - mu*purchasebeta1(k));
        end
        if(M1(i,end-1) == 1)
            lcl(j,2) = lcl(j,2) + log(p);
        elseif(M1(i,end-1) == 0)
            lcl(j,2) = lcl(j,2) + log(1-p);
        end
    end

    
    for i=1:size(M2,1)
        customer = getcustomer(M2,i);
        
        p = sigmoid(purchasebeta2.*customer);
        for k=1:size(purchasebeta2,2)
            purchasebeta2(k) = purchasebeta2(k) + step*((M2(i,end-1) - sigmoid(purchasebeta2.*customer))*customer(k) - mu*purchasebeta2(k));
        end
        if(M2(i,end-1) == 1)
            lcl(j,3) = lcl(j,3) + log(p);
        elseif(M2(i,end-1) == 0)
            lcl(j,3) = lcl(j,3) + log(1-p);
        end 
    end
   
    j
end
figure;
plot(lcl(:,1));
xlabel('Epoch');
ylabel('Log Likelihood');
title('P(purchase = 1 | visit = 1, x, No Email)');
figure;
plot(lcl(:,2));
xlabel('Epoch');
ylabel('Log Likelihood');
title('P(purchase = 1 | visit = 1, x, Mens Email)');
figure;
plot(lcl(:,3));
xlabel('Epoch');
ylabel('Log Likelihood');
title('P(purchase = 1 | visit = 1, x, Womens Email)');



purchaseProbs0 = zeros(size(M0(M0(:,11) == 1),1),1);
otherpProbs0 = zeros(size(M0(M0(:,11) == 0),1),1);
purchaseProbs1 = zeros(size(M1(M1(:,11) == 1),1),1);
otherpProbs1 = zeros(size(M1(M1(:,11) == 0),1),1);
purchaseProbs2 = zeros(size(M2(M2(:,11) == 1),1),1);
otherpProbs2 = zeros(size(M2(M2(:,11) == 0),1),1);
purchasec0 = 1;
otherpc0 = 1;
purchasec1 = 1;
otherpc1 = 1;
purchasec2 = 1;
otherpc2 = 1;

Mv = M(M(:,10)==1,:);
for n=1:size(Mv,1)
    customer = getcustomer(Mv,n);
    purchase = Mv(n,11);
    segment = Mv(n,9);
    
    if(segment == 0)
        if(purchase == 1)
            purchaseProbs0(purchasec0) = sigmoid(purchasebeta0.*customer);
            purchasec0 = purchasec0 + 1;
        else
            otherpProbs0(otherpc0) = sigmoid(purchasebeta0.*customer);
            otherpc0 = otherpc0 + 1;
        end
    end
    if(segment == 1)
        if(purchase == 1)
            purchaseProbs1(purchasec1) = sigmoid(purchasebeta1.*customer);
            purchasec1 = purchasec1 + 1;
        else
            otherpProbs1(otherpc1) = sigmoid(purchasebeta1.*customer);
            otherpc1 = otherpc1 + 1;
        end
    end
    if(segment == 2)
        if(purchase == 1)
            purchaseProbs2(purchasec2) = sigmoid(purchasebeta2.*customer);
            purchasec2 = purchasec2 + 1;
        else
            otherpProbs2(otherpc2) = sigmoid(purchasebeta2.*customer);
            otherpc2 = otherpc2 + 1;
        end
    end
end

purchaseProbs = [mean(otherpProbs0), mean(purchaseProbs0); mean(otherpProbs1), mean(purchaseProbs1); mean(otherpProbs2), mean(purchaseProbs2)]

final0 = zeros(1,size(M,1));
final1 = zeros(1,size(M,1));
final2 = zeros(1,size(M,1));
for n = 1:size(M,1)
    xcustomer = getcustomer(M,n);
    
    expcustomer = xcustomer';%[M_(n,1:19) M_(n,21:end-2) 1]';
    
    
    probvisit0 = sigmoid(visitbeta0.*xcustomer);
    probvisit1 = sigmoid(visitbeta1.*xcustomer);
    probvisit2 = sigmoid(visitbeta2.*xcustomer);
    
    probpurch0 = sigmoid(purchasebeta0.*xcustomer);
    probpurch1 = sigmoid(purchasebeta1.*xcustomer);
    probpurch2 = sigmoid(purchasebeta2.*xcustomer);
    
    expected0 = sum(expcustomer.*b0);
    expected1 = sum(expcustomer.*b1);
    expected2 = sum(expcustomer.*b2);
    
    final0(n) = expected0*probpurch0*probvisit0;
    final1(n) = expected1*probpurch1*probvisit1;
    final2(n) = expected2*probpurch2*probvisit2;
end
finalCashMoney = [mean(final0); mean(final1); mean(final2)]